<template>
  <el-dialog :close-on-click-modal='false' :title="$t('添加')" :v-if="dialogVisible" :visible.sync="dialogVisible" width="500px" v-drag append-to-body @close="close">
    <el-form ref="forms" :model="record" :rules="rules" label-width="100px" style="margin:0 20px;" label-position="left">
      <el-form-item :label="$t('子网')" prop="Ipv4Net">
        <el-input v-emoji v-model="record.Ipv4Net" ref="ipv4Net" size="small"></el-input>
      </el-form-item>
      <el-form-item :label="ipType === 'v4'?$t('子网掩码'):$t('前缀长度')" prop="Netmask">
        <el-input v-emoji v-model="record.Netmask" ref="netMask" size="small"></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submit()" class="innerAddSubmit">{{$t('确 定')}}</el-button>
      <el-button @click="close()" class="innerAddClose">{{$t('取 消')}}</el-button>
    </div>
  </el-dialog>
</template>

<script>
// import { addLocalNetwork, addRemoteNetwork } from '@/api/network/ipsecvpn'
import { inputClass } from '@/utils/input-class.js'
export default {
  name: 'addIpDialog',
  data () {
    var validateName = (rule, value, callback) => {
      if (String(value).trim() === '') {
        callback(new Error(this.$t('不能为空')))
      } else {
        callback()
      }
    }
    return {
      dialogVisible: false,
      record: {
        Index: null,
        Ipv4Net: '',
        Netmask: ''
      },
      recordCopy: {
        Index: null,
        Ipv4Net: '',
        Netmask: ''
      },
      type: null,
      ipType: '',
      rules: {
        Ipv4Net: [{ required: false, validator: validateName, trigger: 'blur' }],
        Netmask: [{ required: false, validator: validateName, trigger: 'blur' }]
      }
    }
  },
  methods: {
    init (type, id, ipType) {
      this.dialogVisible = true
      this.type = type
      this.ipType = ipType
      this.record.Index = id
      this.$nextTick(() => {
        const data = ['ipv4Net', 'netMask']
        inputClass(this.$refs, data)
      })
    },
    submit () {
      this.$refs.forms.validate((valid) => {
        if (valid) {
          // if (this.record.Index) {
          //   if (this.type === 'LocalIp') {
          //     addLocalNetwork(this.record).then(res => {
          //       if (res.status === 0) {
          //         this.$message({
          //           message: this.$t('添加成功'),
          //           type: 'success'
          //         })
          //         this.$emit('getInfoRefresh')
          //         this.close()
          //       }
          //     })
          //   } else {
          //     addRemoteNetwork(this.record).then(res => {
          //       if (res.status === 0) {
          //         this.$message({
          //           message: this.$t('添加成功'),
          //           type: 'success'
          //         })
          //         this.$emit('getInfoRefresh')
          //         this.close()
          //       }
          //     })
          //   }
          // } else {
          this.$emit('ipSubmit', JSON.parse(JSON.stringify(this.record)), this.type)
          this.close()
          // }
        }
      })
    },
    close () {
      this.dialogVisible = false
      this.$refs.forms.resetFields()
      this.type = null
      this.ipType = ''
      this.record = Object.assign({}, this.recordCopy)
    }
  }
}
</script>
